document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('addTimelineForm');
    const imageInput = form.querySelector('input[type="file"]');
    const imagePreview = form.querySelector('.image-preview');
    
    // 图片预览
    imageInput.addEventListener('change', function() {
        imagePreview.innerHTML = '';
        if (this.files.length > 3) {
            alert('最多只能上传3张图片');
            this.value = '';
            return;
        }
        
        for (let file of this.files) {
            const reader = new FileReader();
            reader.onload = function(e) {
                const img = document.createElement('img');
                img.src = e.target.result;
                imagePreview.appendChild(img);
            }
            reader.readAsDataURL(file);
        }
    });
    
    // 表单提交
    form.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const formData = new FormData(this);
        const coupleId = window.location.pathname.split('/')[3];
        
        fetch(`/user/couple/${coupleId}/timeline/add`, {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('添加成功');
                window.location.href = `/couples/${coupleId}`;
            } else {
                alert(data.message || '添加失败');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('添加失败，请重试');
        });
    });
}); 